package com.lzs.bootsafety.business.sys.service.impl;

import com.lzs.bootsafety.business.sys.entity.SysMenu;
import com.lzs.bootsafety.business.sys.mapper.SysMenuMapper;
import com.lzs.bootsafety.business.sys.service.SysMenuService;
import com.lzs.bootsafety.common.util.MenuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public List<SysMenu> getListMenuByUserId(Long userId,String type) {
        return sysMenuMapper.selectListMenuByUserId(userId,type);
    }

    @Override
    public List<SysMenu> getList(SysMenu sysMenu) {
        return sysMenuMapper.selectList(sysMenu);
    }

    @Override
    public List<SysMenu> getChildrenByMenuId(Long menuId) {
        return sysMenuMapper.selectChildrenByMenuId(menuId);
    }

    @Override
    public int delByPrimaryKey(Long menuId) {
        return sysMenuMapper.deleteByPrimaryKey(menuId);
    }

    @Override
    public int modifyByPrimaryKeySelective(SysMenu record) {
        List<SysMenu> sysMenuList = sysMenuMapper.selectList(null);
        List<SysMenu> sysMenus=MenuTree.buildTree(sysMenuList, record.getMenuId());
        //递归更新-->父节点禁用下面的子节点也全部禁用
        sysMenus.forEach((sysMenu)->{
            sysMenu.setStatus(record.getStatus());
            sysMenu.setUpdateTime(new Date());
            sysMenu.setUpdatePeople(record.getUpdatePeople());
            sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
        });
        return sysMenuMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int addSelective(SysMenu record) {
        return sysMenuMapper.insertSelective(record);
    }

    @Override
    public SysMenu getSysMenuInfo(Long menuId) {
        return sysMenuMapper.selectSysMenuInfo(menuId);
    }


}
